package com.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.InterpolationMethod;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	public class LoadProgressBar extends Sprite
	{
		private var _stage:Stage;
		private var _thisWidth:int;
		private var _thisHeight:int;
		private var _bgContain:Sprite;
		private var _progressContain:Sprite;
		private var _progressBgShape:Shape;
		private var _progressShape:Shape;
		private var _txtProgress:TextField;
		private var _txtTip:TextField;
		private var _bytesLoaded:int;
		private var _bytesAll:int;
		private var _preLoadBtyes:int;
		private var _preLoadTime:Number = 0;
		private var _loadedFileNum:int = 0;
		private var _totalFileNum:int = 1;
		private var _loadFileName:String;
		private var _loadSpeed:Number = 0; 
		private var _matrix:Matrix;
		
		public function LoadProgressBar(stage:Stage)
		{
			_stage = stage;
			
			initContain();
			drawBg();
			setThisCoordinates();
		}
		
		private function initContain():void
		{
			_bgContain = new Sprite(); 
			this.addChild(_bgContain);
			_progressContain = new Sprite(); 
			this.addChild(_progressContain);
		}
		
		/**设置进度条显示位置*/		
		private function setThisCoordinates():void
		{
			this.x = (_stage.stageWidth - _bgContain.width) /2;
			this.y = (_stage.stageHeight - _bgContain.height) /2;
		}
		
		private  function drawBg():void
		{
			_thisWidth = _stage.stageWidth / 2;
			_thisWidth > 400 ? _thisWidth = 500:_thisWidth = _thisWidth;
			_thisHeight = 26;
			drawGradientShape(_bgContain,_thisWidth,_thisHeight,0,0,Math.PI / 2,0,0xFF0000,0,20,20,'linear',[0x424F58,0x111217],[1,1],[0,255]);
			
			_progressBgShape = new Shape();
			_progressContain.addChild(_progressBgShape);
			drawShape(_progressBgShape,_thisWidth-10,_thisHeight-10,1,0xFFFFFF,.2,0,1,5,5);
			_progressBgShape.x = (_bgContain.width - _progressBgShape.width)/2;
			_progressBgShape.y = (_bgContain.height - _progressBgShape.height)/2;
		}
		
		/**
		 * 显示加载进度
		 * @param loadProgress 当前文件加载进度 %
		 * @param totalProgress 总文件加载进度 %
		 * @param loadSpeed 下载速度 KB/s
		 * @param loadFileNum 已加载的文件数
		 * @param totalFileNum 需要加载的文件总数
		 * @param loadFileName 当前加载的文件名
		 */		
		public function loadProgress(bytesLoaded:int,bytesAll:int,totalFileNum:int = 1,loadFileName:String = '请耐心等待'):void
		{
			_bytesLoaded = bytesLoaded;
			_bytesAll = bytesAll;
			_totalFileNum = totalFileNum;
			_loadFileName = loadFileName;
			
			//（当前已加载 - 之前加载） / 1024 = 间隔时间加载的KB
			_loadSpeed = ((_bytesLoaded - _preLoadBtyes)/1024) / ((new Date().getTime() - _preLoadTime)/1000);
			if(String(_loadSpeed) == "Infinity") _loadSpeed = 1000;
			
			if(_bytesLoaded >= _bytesAll) ++_loadedFileNum;
			if(!_progressShape) setProgressShape();
			refreshProgress(_progressBgShape.width * (_bytesLoaded / _bytesAll));
			addProgressShow();
			setTxtContain();
			_preLoadBtyes = _bytesLoaded >= _bytesAll ? _bytesLoaded : _preLoadBtyes = 0;
			_preLoadTime = new Date().getTime();
			
		}
		
		/**设置进度条长度*/		
		private function setProgressShape():void{
			_progressShape = new Shape();
			_progressContain.addChild(_progressShape);
			_progressShape.x = _progressBgShape.x + .5;
			_progressShape.y = _progressBgShape.y + .5;
		}
		
		/**刷新进度 */		
		public function refreshProgress(width:int):void
		{
			//drawGradientShape(_progressShape,width,_progressBgShape.height-2,0,0,Math.PI / 2,0,0x116089,0,5,5,'linear',[0xcc0000,0x330000,0xcc0000],[1,1,1],[0,122,255]);
			drawGradientShape(_progressShape,width,_progressBgShape.height-2,0,0,Math.PI / 2,0,0x116089,0,5,5,'linear',[0x00FFFF,0x0099FF,0x00FFFF],[1,1,1],[0,122,255]);
		}
		
		private function setTxtContain():void
		{
			_txtProgress.htmlText = htmlTxt('当前文件加载进度:' + (100 * _bytesLoaded / _bytesAll).toFixed(1) + '%' +'    ' + _loadSpeed.toFixed(1) + 'KB/S','#FFCC00',12);
			_txtProgress.width = _txtProgress.textWidth + 10;
			_txtProgress.x = (_thisWidth - _txtProgress.textWidth)/2;
			_txtProgress.y = _progressShape.y - 2;
			
			_txtTip.htmlText = htmlTxt('正在努力加载,'+htmlTxt(_loadFileName) + '.总进度:(' +_loadedFileNum +'/' + _totalFileNum + ')  ' + (100 * _loadedFileNum / _totalFileNum).toFixed(1) + '%');
			_txtTip.width = _txtTip.textWidth + 10;
			_txtTip.x = (_thisWidth - _txtTip.textWidth) / 2;
		}
		
		private function addProgressShow():void
		{
			if(_txtProgress) return;
			_txtProgress = new TextField();
			this.addChild(_txtProgress);
			_txtProgress.filters = [new DropShadowFilter(0, 45, 0, 1, 2, 2, 10)];
			_txtTip = new TextField();
			this.addChild(_txtTip);
			_txtTip.y = _thisHeight + 10;
		}
		
		public function dispose():void
		{
			clearTarget(_progressBgShape);
			clearTarget(_progressShape);
			clearTarget(_txtProgress);
			clearTarget(_txtTip);
			clearTarget(_bgContain);
			clearTarget(_progressContain);
			while(numChildren) removeChildAt(0);
		}
		
		private function clearTarget(target:DisplayObject):void{
			target.parent.removeChild(target);
			target = null;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * 绘制渐变色条
		 * @param target 目标
		 * @param width 宽度
		 * @param heigth 高度
		 * @param matrixOffsetX X方向位移
		 * @param matrixOffsetY Y方向位移
		 * @param matrixRotation 旋转角度 单位：弧度
		 * 
		 * @param lineThickness 边缘线粗度
		 * @param lineColor 颜色
		 * @param lineAlpha 透明度
		 * @param ellipseWidth 横向圆角大小
		 * @param ellipseHeight 纵向圆角大小
		 * 
		 * @param fillType 渐变类型 linear/radial
		 * @param colors 渐变色数组 [0xcc0000,0x330000,0xcc0000]
		 * @param alphas 透明度数组 [1,1,1]
		 * @param ratios 颜色分布比率 [0,122,255]
		 * @param spreadMethod 
		 * @param interpolationMethod
		 * @param focalPointRatio
		 */
		private function drawGradientShape(target:*,width:int,heigth:int,matrixOffsetX:int = 0,matrixOffsetY:int = 0,matrixRotation:Number = Math.PI / 2,
										   lineThickness:Number = 1,lineColor:uint = 0,lineAlpha:Number = 1,ellipseWidth:int = 20,ellipseHeight:int= 20,
										   fillType:String='linear',colors:Array = null,alphas:Array = null,ratios:Array = null,spreadMethod:String="pad", interpolationMethod:String="rgb", focalPointRatio:Number=0):void
		{
			if(!_matrix) _matrix = new Matrix();
			_matrix.createGradientBox(width,heigth,matrixRotation,matrixOffsetX,matrixOffsetY);
			target.graphics.clear();
			target.graphics.lineStyle(lineThickness,lineColor,lineAlpha);
			target.graphics.beginGradientFill(fillType,colors,alphas,ratios,_matrix,'pad','rgb',0);
			target.graphics.drawRoundRect(0,0,width,heigth,ellipseWidth,ellipseHeight);
			target.graphics.endFill();
		}
		
		private function drawShape(target:*,width:int,heigth:int, lineThickness:Number = 1,lineColor:uint = 0,lineAlpha:Number = 1,color:uint = 0,alpha:Number = 1,ellipseWidth:int = 20,ellipseHeight:int= 20):void{
			target.graphics.clear();
			target.graphics.lineStyle(lineThickness,lineColor,lineAlpha);
			target.graphics.beginFill(color,alpha);
			target.graphics.drawRoundRect(0,0,width,heigth,ellipseWidth,ellipseHeight);
			target.graphics.endFill();
		}
		
		private function htmlTxt(source:*,color:String = '#FFFFFF',size:uint = 14,font:String = 'Verdana'):String
		{
			return '<font color="' + color +'" size="' + size + '" font="' + font +'">' + source + '</font>'
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}